<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="content-type"
 content="text/html; charset=ISO-8859-1">
  <title>A Sample with JAXB Binding Compiler XJC</title>
</head>
<body>
<h2>A Sample with&nbsp; JAXB Binding Compiler <span
 style="font-style: italic;">XJC</span><br>
</h2>
This
sample application demonstrates how to construct value classes and
create a java content tree from scratch and marshal it to XML data step
by step.<br>
<h3>System Requests</h3>
<ul>
  <li>Download and install the <a
 href="http://java.sun.com/webservices/downloads/webservicespack.html">Java
Web Services Developer Pack (Java WSDP)</a> currently at version 2.0
from Sun Microsystem. The Java WSDP is a free, integrated toolkit that
allows Java developers to build, test, and deploy XML applications, Web
services, and Web applications. Assume that you install JWSDP to
C:\jwsdp-2.0 directory.<br>
  </li>
</ul>
<ul>
  <li>Create a C:\jaxb_samples directory on your machine.<br>
  </li>
</ul>
<ul>
  <li>For occasional developers, it is convenient to set these
variables in a batch file that you run every time you open a
command-prompt window. Alternatively, these settings can be permanently
added to the command-prompt shortcut. Here is what you should add to a
&#8220;jaxbsetup.bat&#8221; file in C:\jaxb_samples directory (note that only &#8220;set&#8221;
commands are in this listing, any line that doesn&#8217;t start with &#8220;set&#8221; is
a continuation of the previous line):<br>
    <br>
    <table cellpadding="0" cellspacing="0" border="0"
 style="text-align: left; width: 100%;">
      <tbody>
        <tr>
          <td style="vertical-align: top; width: 100%;"><code>set
JWSDP_HOME=C:\jwsdp-2.0<br>
set JWSDP_JAXB_LIB=%JWSDP_HOME%\jaxb\lib<br>
set JWSDP_SHARED_LIB=%JWSDP_HOME%\jwsdp-shared\lib<br>
set JWSDP_SJSXP_LIB=%JWSDP_HOME%\sjsxp\lib<br>
          <br>
set CLASSPATH=%JWSDP_JAXB_LIB%\jaxb-api.jar;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
%JWSDP_JAXB_LIB%\jaxb-impl.jar;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
%JWSDP_JAXB_LIB%\jaxb1-impl.jar;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
%JWSDP_JAXB_LIB%\jaxb-xjc.jar;%CLASSPATH%<br>
          <br>
set CLASSPATH=%JWSDP_SJSXP_LIB%\sjsxp.jar;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
%JWSDP_SJSXP_LIB%\jsr173_api.jar;%CLASSPATH%<br>
          <br>
set CLASSPATH=%JWSDP_SHARED_LIB%\activation.jar;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
%JWSDP_SHARED_LIB%\resolver.jar;%CLASSPATH%<br>
set PATH=</code><code>%JWSDP_HOME%\jaxb\bin;</code><code>%JWSDP_HOME%\jwsdp-shared\bin;%PATH%</code></td>
        </tr>
      </tbody>
    </table>
    <br>
  </li>
  <br>
  <li>Copy <code>%JWSDP_HOME%\jaxb\samples\create-marshal </code>directory
to <span style="font-style: italic;">jaxb_smaple</span> directory.
(Note: we&nbsp; will not use ant to build this project in here. You can
delete <span style="font-style: italic;">build.xml</span> file).<code><br>
    </code></li>
  <li>Create a subdirectory called <span style="font-style: italic;">classes</span>
under <span style="font-style: italic;">jaxb_sample\create-marshal.</span></li>
</ul>
<br>
<h3>JAXB Binding Compiler <span style="font-style: italic;">xjc</span><br>
</h3>
<p> </p>
<!--  sidebar begins  --><!--  don't move sidebars  -->
<p>The <a href="http://java.sun.com/xml/jaxb/">Java Architecture for
XML
Binding
(JAXB)</a> provides a <span style="font-style: italic;">JAXB binding
compiler</span>&nbsp;<span style="font-weight: bold;"> xjc</span>. The <i>JAXB
binding compiler </i>takes XML schema as input, and then generates a
package of Java classes and interfaces that reflect the rules defined
in the source schema. These generated classes and interfaces are in
turn compiled and combined with a set of common JAXB utility packages
to provide a <i>JAXB binding framework</i>. The Java
classes generated with the <i>JAXB binding compiler&nbsp; </i><span
 style="font-weight: bold;">xjc</span> utility represent the
different elements and <span style="font-style: italic;">complexType</span>(s)&nbsp;
in an XML Schema. An XML document that conforms to the XML
Schema may be constructed from the Java classes.<br>
</p>
<p>JAXB generates Java classes and interfaces corresponding to the
top-level elements and top-level <span style="font-style: italic;">complexType</span><code></code>
elements. In
a XML Schema, an element is represented with <span
 style="font-style: italic;">&lt;xs:element/&gt;</span><code
 style="font-style: italic;"></code>,
and a <span style="font-style: italic;">complexType</span><code> </code>is
represented with <span style="font-style: italic;">&lt;xs:complexType/&gt;</span><code
 style="font-style: italic;"></code>. The following example schema <span
 style="font-style: italic;">po.xsd</span> has top-level element and <span
 style="font-style: italic;">complexType </span>declarations (under
&lt;JWSDP installed directory&gt;\jaxb\samples\create-marshal\): </p>
<code>&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;<br>
&nbsp; &lt;xsd:element name="purchaseOrder"
type="PurchaseOrderType"/&gt;<br>
&nbsp; &lt;xsd:element name="comment" type="xsd:string"/&gt;<br>
&nbsp; <span style="font-weight: bold;">&lt;xsd:complexType
name="PurchaseOrderType"&gt;</span><br>
&nbsp;&nbsp;&nbsp; &lt;xsd:sequence&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="shipTo"
type="USAddress"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="billTo"
type="USAddress"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element ref="comment"
minOccurs="0"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="items"
type="Items"/&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;/xsd:sequence&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;xsd:attribute name="orderDate"
type="xsd:date"/&gt;<br>
&nbsp; &lt;/xsd:complexType&gt;<br>
<br>
&nbsp; <span style="font-weight: bold;">&lt;xsd:complexType
name="USAddress"&gt;</span><br>
&nbsp;&nbsp;&nbsp; &lt;xsd:sequence&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="name"
type="xsd:string"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="street"
type="xsd:string"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="city"
type="xsd:string"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="state"
type="xsd:string"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="zip"
type="xsd:decimal"/&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;/xsd:sequence&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;xsd:attribute name="country" type="xsd:NMTOKEN"
fixed="US"/&gt;<br>
&nbsp; &lt;/xsd:complexType&gt;<br>
<br>
&nbsp; <span style="font-weight: bold;">&lt;xsd:complexType
name="Items"&gt;</span><br style="font-weight: bold;">
&nbsp;&nbsp;&nbsp; &lt;xsd:sequence&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:element name="item"
minOccurs="1" maxOccurs="unbounded"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:complexType&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:sequence&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:element name="productName" type="xsd:string"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:element name="quantity"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:simpleType&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:restriction base="xsd:positiveInteger"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:maxExclusive value="100"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/xsd:restriction&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/xsd:simpleType&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/xsd:element&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:element name="USPrice" type="xsd:decimal"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:element ref="comment" minOccurs="0"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:element name="shipDate" type="xsd:date" minOccurs="0"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/xsd:sequence&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;xsd:attribute name="partNum" type="SKU" use="required"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/xsd:complexType&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/xsd:element&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;/xsd:sequence&gt;<br>
&nbsp; &lt;/xsd:complexType&gt;<br>
<br>
&nbsp; &lt;!-- Stock Keeping Unit, a code for identifying products
--&gt;<br>
&nbsp; &lt;xsd:simpleType name="SKU"&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;xsd:restriction base="xsd:string"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsd:pattern
value="\d{3}-[A-Z]{2}"/&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;/xsd:restriction&gt;<br>
&nbsp; &lt;/xsd:simpleType&gt;<br>
<br>
&lt;/xsd:schema&gt;<br>
</code><br>
The problems with JAXB began when the JSR 31 expert group decided that
they did not want to attempt full compatibility with the XML Schema
standard. It is explicitly not a goal of JAXB to support all of Schema
- they even have a list of key Schema constructs which they explicitly
do not require. The expert group tried to assuage themselves by
assuring, in the specification, that although all of XML Schema was not
required, it would be permitted.&nbsp; If such
unsupported Schema constructs are included in a schema, an error will
be
generated when you try to generate Java classes from them with JAXB
binding compiler <span style="font-style: italic;">xjc</span>.
<br>
<br>
The following schema elements are not supported: <span
 style="font-style: italic;">xs:any</span>, <span
 style="font-style: italic;">xs:anyAttribute</span>,
<span style="font-style: italic;">xs:notation</span>, <span
 style="font-style: italic;">xs:redefine</span>, <span
 style="font-style: italic;">xs:key</span>,
<span style="font-style: italic;">xs:keyref</span>, and <span
 style="font-style: italic;">xs:unique</span>. The following
schema attributes are not supported: <span style="font-style: italic;">complexType.abstract</span>,
<span style="font-style: italic;">element.abstract</span>, <span
 style="font-style: italic;">element.substitutionGroup</span>,
<span style="font-style: italic;">xsi:type</span>, <span
 style="font-style: italic;">complexType.block</span>, <span
 style="font-style: italic;">complexType.final</span>,
<span style="font-style: italic;">element.block</span>, <span
 style="font-style: italic;">element.final</span>, <span
 style="font-style: italic;">schema.blockDefault</span>,
and <span style="font-style: italic;">schema.finalDefault</span>.<br>
<h3>Generating Java Classes</h3>
<p>The&nbsp; <a
 href="http://java.sun.com/webservices/docs/2.0/jaxb/xjc.html"
 style="font-style: italic;">xjc</a>
utility is run on the schema to bind a schema to Java classes. Run the <span
 style="font-style: italic;">xjc
</span>utility on the example schema with the command: "<span
 style="font-style: italic;">xjc po.xsd</span>". More detail about the
the options of the xjc utility, please&nbsp; read&nbsp; <a
 href="http://java.sun.com/webservices/docs/2.0/jaxb/xjc.html">Java
Architecture for XML Binding Binding Compiler (xjc)</a>. <code></code><br>
</p>
<p>Let's go to <span style="font-style: italic;">C:\jaxb_samples\create-marshal</span>,
run the <span style="font-style: italic;">xjc</span> utility on the
example schema <span style="font-style: italic;">po.xsd</span>. <br>
</p>
<code>C:\jaxb_samples\create-marshal&gt;xjc -p primer.po -d src po.xsd<br>
parsing a schema...<br>
compiling a schema...<br>
primer\po\Items.java<br>
primer\po\ObjectFactory.java<br>
primer\po\PurchaseOrderType.java<br>
primer\po\USAddress.java</code><br>
<p>The<span style="font-style: italic;"> primer.po</span> is the
package name&nbsp; and the <span style="font-style: italic;">src</span>
is the generated codes outputing directory name.<br>
</p>
<p>A factory class (<span style="font-style: italic;">ObjectFactory.java</span>),
consisting of methods to create instance objects, also gets generated. <br>
</p>
<p>A Java class is generated corresponding to each top-level <span
 style="font-style: italic;">xs:complexType</span>
in the example XML Schema.<span style="font-style: italic;"> </span>For
example, <span style="font-style: italic;">USAddress.java</span> is <span
 style="font-style: italic;">USAddress</span> class generated
corresponding
to the top-level element <span style="font-style: italic;">USAddress</span><code></code>.
The <span style="font-style: italic;">USAddress.java</span> is
illustrated in the listing below:
</p>
<pre><code>package primer.po;<br><br>import java.math.BigDecimal;<br>import javax.xml.bind.annotation.AccessType;<br>import javax.xml.bind.annotation.XmlAccessorType;<br>import javax.xml.bind.annotation.XmlAttribute;<br>import javax.xml.bind.annotation.XmlType;<br>import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;<br>import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;<br>import primer.po.USAddress;<br><br><br>@XmlAccessorType(AccessType.FIELD)<br>@XmlType(name = "USAddress", propOrder = {<br>    "name",<br>    "street",<br>    "city",<br>    "state",<br>    "zip"<br>})<br>public class USAddress {<br><br>    protected String name;<br>    protected String street;<br>    protected String city;<br>    protected String state;<br>    protected BigDecimal zip;<br>    @XmlAttribute<br>    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)<br>    protected String country;<br><br>    public String getName() {<br>        return name;<br>    }<br><br>    public void setName(String value) {<br>        this.name = value;<br>    }<br><br>    public String getStreet() {<br>        return street;<br>    }<br><br>    public void setStreet(String value) {<br>        this.street = value;<br>    }<br><br>    public String getCity() {<br>        return city;<br>    }<br><br>    public void setCity(String value) {<br>        this.city = value;<br>    }<br><br>    public String getState() {<br>        return state;<br>    }<br><br>    public void setState(String value) {<br>        this.state = value;<br>    }<br><br>    public BigDecimal getZip() {<br>        return zip;<br>    }<br><br>    public void setZip(BigDecimal value) {<br>        this.zip = value;<br>    }<br><br>    public String getCountry() {<br>        if (country == null) {<br>            return "US";<br>        } else {<br>            return country;<br>        }<br>    }<br><br>    public void setCountry(String value) {<br>        this.country = value;<br>    }<br><br>}<br><br></code></pre>
<h3>Creating an XML Document from the Java Classes</h3>
<p>Let's take a look the <span style="font-style: italic;">Main.java</span>
code. The original code output XML Document to console and we made a
little changes to make it output to an XML Document <span
 style="font-style: italic;">po.xml</span>. The bold lines are what we
add or modify lines.<br>
</p>
<pre><code><span style="font-weight: bold;">import java.io.FileNotFoundException;</span><br
 style="font-weight: bold;"><span style="font-weight: bold;">import java.io.FileOutputStream;</span><br
 style="font-weight: bold;">import java.math.BigDecimal;<br>import java.math.BigInteger;<br>import java.util.GregorianCalendar;<br>import java.util.List;<br><br>import javax.xml.bind.JAXBContext;<br>import javax.xml.bind.JAXBElement;<br>import javax.xml.bind.JAXBException;<br>import javax.xml.bind.Marshaller;<br><br>import javax.xml.datatype.DatatypeFactory;<br>import javax.xml.datatype.XMLGregorianCalendar;<br>import javax.xml.datatype.DatatypeConfigurationException;<br><br>// import java content classes generated by binding compiler<br>import primer.po.*;<br> <br>public class Main {<br>    <br>    public static void main( String[] args ) {<br>        try {<br>            // create a JAXBContext. </code>A <code>JAXBContext </code>object is required to implement the JAXB binding framework <br>            // operations <code>marshal</code>,u<code>nmarshal</code>, and <code>validate</code>. An application creates a new instance (object) of <br>            // the <code>JAXBContext</code> class with the static method <code>newInstance(String contextPath)</code>.<br>            // The <code>contextPath</code> specifies a list of Java package names for the schema-derived classes. <br>            // The directory <span
 style="font-style: italic;">primer/po/</span> contains the JAXB-generated classes.</pre>
<pre><code>            JAXBContext jc = JAXBContext.newInstance( "primer.po" );<br>            <br>            // create an empty PurchaseOrder<br>            PurchaseOrderType po = new PurchaseOrderType();<br>            <br>            // set the required orderDate attribute<br>            po.setOrderDate( getDate() );<br>            <br>            // create shipTo USAddress object<br>            USAddress shipTo = createUSAddress( "Alice Smith",<br>                                                "123 Maple Street",<br>                                                "Cambridge",<br>                                                "MA",<br>                                                "12345" );<br>                                                <br>            // set the required shipTo address <br>            po.setShipTo( shipTo );<br>            <br>            // create billTo USAddress object<br>            USAddress billTo = createUSAddress( "Robert Smith",<br>                                                "8 Oak Avenue",<br>                                                "Cambridge",<br>                                                "MA",<br>                                                "12345" );<br>            <br>            // set the requred billTo address<br>            po.setBillTo( billTo );<br>                                                <br>            // create an empty Items object<br>            Items items = new Items();<br>            <br>            // get a reference to the ItemType list<br>            List&lt;Items.Item&gt; itemList = items.getItem();<br>            <br>            // start adding ItemType objects into it<br>            itemList.add( createItem( "Nosferatu - Special Edition (1929)", <br>                                      new BigInteger( "5" ), <br>                                      new BigDecimal( "19.99" ), <br>                                      null,<br>                                      null,<br>                                      "242-NO" ) );<br>            itemList.add( createItem( "The Mummy (1959)", <br>                                      new BigInteger( "3" ), <br>                                      new BigDecimal( "19.98" ), <br>                                      null,<br>                                      null,<br>                                      "242-MU" ) );<br>            itemList.add( createItem( "Godzilla and Mothra: Battle for Earth/Godzilla vs. King Ghidora", <br>                                      new BigInteger( "3" ), <br>                                      new BigDecimal( "27.95" ), <br>                                      null,<br>                                      null,<br>                                      "242-GZ" ) );<br>            <br>            // set the required Items list<br>            po.setItems( items );<br>           <br>	    // create an element for marshalling<br>	    JAXBElement&lt;PurchaseOrderType&gt; poElement = (new ObjectFactory()).createPurchaseOrder(po);<br><br></code>            // Create a <code>Marshaller</code> with the <code>createMarshaller</code> method. The <code>Marshaller</code> class has overloaded <code>marshal</code><br>            // methods to marshal (that is, convert a Java object to XML data) into SAX2 events, a Document Object<br>            // Model (DOM) structure, an <code>OutputStream</code>, a <code>javax.xml.transform.Result</code>, or a <code>java.io.Writer</code> object.<br><br><code>            Marshaller m = jc.createMarshaller();<br>            m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );<br></code></pre>
<pre>            //Marshal the <code></code> <code>poElement</code> object to an XML document with the <code>marshal</code> method of the class <code>Marshaller</code>. <br>            //The <code>poElement</code> object is marshalled to an <code>OutputStream</code>.<br><code
 style="font-weight: bold;">            m.marshal( poElement, </code><code
 style="font-weight: bold;">new FileOutputStream("po.xml")</code><code><span
 style="font-weight: bold;">);</span><br style="font-weight: bold;"><span
 style="font-weight: bold;">            </span><br
 style="font-weight: bold;"><span style="font-weight: bold;">        }  catch( FileNotFoundException fe) {</span><br
 style="font-weight: bold;"><span style="font-weight: bold;">           fe.printStackTrace();</span><br
 style="font-weight: bold;">        } catch( JAXBException je ) {<br>            je.printStackTrace();<br>        } <br>    }<br>    <br>    public static USAddress createUSAddress(  String name, String street,<br>                                              String city, String state,<br>                                              String zip ) {<br>    <br>        // create an empty USAddress objects                                             <br>        USAddress address = new USAddress();<br>       <br>        // set properties on it<br>        address.setName( name );<br>        address.setStreet( street );<br>        address.setCity( city );<br>        address.setState( state );<br>        address.setZip( new BigDecimal( zip ) );<br>        <br>        // return it<br>        return address;<br>    }<br>    <br>    public static Items.Item createItem( String productName,<br>                                         BigInteger quantity,<br>                                         BigDecimal price,<br>                                         String comment,<br>                                         XMLGregorianCalendar shipDate,<br>                                         String partNum ) {<br>   <br>        // create an empty ItemType object<br>        Items.Item item = new Items.Item();<br>        <br>        // set properties on it<br>        item.setProductName( productName );<br>        item.setQuantity( quantity );<br>        item.setUSPrice( price );<br>        item.setComment( comment );<br>        item.setShipDate( shipDate );<br>        item.setPartNum( partNum );<br>        <br>        // return it<br>        return item;<br>    }<br>                                           <br>                                    <br>    private static XMLGregorianCalendar getDate() {<br>	try {<br>	    return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());<br>	} catch (DatatypeConfigurationException e) {<br>	    throw new Error(e);<br>	}<br>    }<br>}<br></code></pre>
<p>Let's compile Main.java with&nbsp; the generated java codes and run
it.<br>
<code>C:\jaxb_samples\create-marshal&gt;javac -d classes src/*.java
src/primer/po/*.java<br>
</code><code>C:\jaxb_samples\create-marshal&gt;cd classes<br>
</code><code>C:\jaxb_samples\create-marshal\classes&gt;java Main<br>
</code><br>
An XML document shall be created after runing Main. The example XML
document, <i>po.xml</i>,
is illustrated in the following listing. <a name="DocumentGenerated"></a></p>
<pre><code>&lt;?xml version="1.0"?&gt;<br>&lt;purchaseOrder orderDate="1999-10-20"&gt;<br>    &lt;shipTo country="US"&gt;<br>        &lt;name&gt;Alice Smith&lt;/name&gt;<br>        &lt;street&gt;123 Maple Street&lt;/street&gt;<br>        &lt;city&gt;Cambridge&lt;/city&gt;<br>        &lt;state&gt;MA&lt;/state&gt;<br>        &lt;zip&gt;12345&lt;/zip&gt;<br>    &lt;/shipTo&gt;<br>    &lt;billTo country="US"&gt;<br>        &lt;name&gt;Robert Smith&lt;/name&gt;<br>        &lt;street&gt;8 Oak Avenue&lt;/street&gt;<br>        &lt;city&gt;Cambridge&lt;/city&gt;<br>        &lt;state&gt;MA&lt;/state&gt;<br>        &lt;zip&gt;12345&lt;/zip&gt;<br>    &lt;/billTo&gt;<br>    &lt;items&gt;<br>    	&lt;item partNum="242-NO" &gt;<br>	    	&lt;productName&gt;Nosferatu - Special Edition (1929)&lt;/productName&gt;<br>	    	&lt;quantity&gt;5&lt;/quantity&gt;<br>	    	&lt;USPrice&gt;19.99&lt;/USPrice&gt;<br>	    &lt;/item&gt;<br>    	&lt;item partNum="242-MU" &gt;<br>	    	&lt;productName&gt;The Mummy (1959)&lt;/productName&gt;<br>	    	&lt;quantity&gt;3&lt;/quantity&gt;<br>	    	&lt;USPrice&gt;19.98&lt;/USPrice&gt;<br>	    &lt;/item&gt;<br>    	&lt;item partNum="242-GZ" &gt;<br>	    	&lt;productName&gt;Godzilla and Mothra: Battle for Earth/Godzilla vs. King Ghidora&lt;/productName&gt;<br>	    	&lt;quantity&gt;3&lt;/quantity&gt;<br>	    	&lt;USPrice&gt;27.95&lt;/USPrice&gt;<br>	    &lt;/item&gt;<br>	&lt;/items&gt;<br>&lt;/purchaseOrder&gt;<br><br></code></pre>
</body>
</html>
